package com.hd.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.usercenter.model.domain.User;
import com.hd.usercenter.mapper.UserMapper;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author 86137
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-06-17 15:52:40
*/

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.usercenter.mapper.UserMapper;
import com.hd.usercenter.model.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 针对表【user】的数据库操作Service实现
 *
 * @author 86137
 * @createDate 2024-06-17 15:52:40
 */
@Log4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{
    private static final String SALT = "kiki";
    @Resource
    private UserMapper userMapper;
    @Override
    public long userRegister(String userAccout, String userPassword, String checkPassword) {
        //1.检验密码
        if(StringUtils.isAllBlank(userAccout,userPassword,checkPassword)){
            return -1;
        }
        //2.账户不合法
        if(userAccout.length()<4){
            return -1;
        }
        //3.密码不合法
        if(userPassword.length()<8 || checkPassword.length()<8){
            return -1;
        }
        //5.账户不包含特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccout);
        if(!matcher.find()){
            return -1;
        }
        //6.密码和校验密码相同
        if(!userPassword.equals(checkPassword)){
            return -1;
        }
        //4.账户不能重复,查表最后执行
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccout",userAccout);
        long count = userMapper.selectCount(queryWrapper);
        if(count > 0){
            return -1;
        }
        //2.加密
        final String SALT = "kiki";
        String encrynewPassword = DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());
        //3.插入数据
        User user = new User();
        user.setUserAccount(userAccout);
        user.setPassword(encrynewPassword);
        boolean saveResult = this.save(user);
        if(!saveResult){
            return -1;
        }
        return user.getId();
    }
    @Override
    public long userLogin(String userAccout, String userPassword, HttpServletRequest request) {

        //2.账户不合法
        if(userAccout.length()<4){
            return -1;
        }
        //3.密码不合法
        if(userPassword.length()<8){
            return -1;
        }
        //5.账户不包含特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccout);
        if(!matcher.find()){
            return -1;
        }
        //2.加密
        String encrynewPassword = DigestUtils.md5DigestAsHex((SALT+userPassword).getBytes());
        //4.账户不能重复,查表最后执行
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userAccout",userAccout);
        queryWrapper.eq("userPassword",userPassword);
        long count = userMapper.selectCount(queryWrapper);
        User user = new User();
        if(user == null){
            System.out.println("user failed UserAccount cannot match userPassword");
            return -1;
        }
        //3.插入数据

        user.setUserAccount(userAccout);
        user.setPassword(encrynewPassword);
        boolean saveResult = this.save(user);
        if(!saveResult){
            return -1;
        }
        //3.用户拖敏
        User safatyUser = new User();
        safatyUser.setId(user.getId());
        safatyUser.setUsername(user.getUsername());
        safatyUser.setUserAccount(user.getUserAccount());
        safatyUser.setAvatarUrl(user.getAvatarUrl());
        safatyUser.setGender(user.getGender());
        safatyUser.setPhone(user.getPhone());
        safatyUser.setEmail(user.getEmail());
        return user.getId();
    }
}


